/*
========================================================================
 Name        : WeatherForecastContainer.cpp
 Author      : Song Yupeng
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/

#include <barsread.h>
#include <stringloader.h>
#include <eiklabel.h>
#include <eikenv.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <WeatherForecast.rsg>
#include <eikbtgpc.h>
#include <aknsskininstance.h>
#include <aknsutils.h>
#include <aknsdrawutils.h>

#include "WeatherForecastContainer.h"
#include "WeatherForecastContainerView.h"
#include "WeatherForecast.hrh"

#include <WeatherSession.h>
#include <WeatherAndCity.h>

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CWeatherForecastContainer::CWeatherForecastContainer()
	{
	iLabel = NULL;
	iLabelWeather = NULL;
	iButtonBeijing = NULL;
	iButtonShenyang = NULL;
	}
/** 
 * Destroy child controls.
 */
CWeatherForecastContainer::~CWeatherForecastContainer()
	{
	delete iLabel;
	iLabel = NULL;
	delete iLabelWeather;
	iLabelWeather = NULL;
	delete iButtonBeijing;
	iButtonBeijing = NULL;
	delete iButtonShenyang;
	iButtonShenyang = NULL;
	delete iBgContext;
	iBgContext = NULL;
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CWeatherForecastContainer
 */
CWeatherForecastContainer* CWeatherForecastContainer::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CWeatherForecastContainer* self = CWeatherForecastContainer::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CWeatherForecastContainer
 */
CWeatherForecastContainer* CWeatherForecastContainer::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CWeatherForecastContainer* self = new ( ELeave ) CWeatherForecastContainer();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CWeatherForecastContainer::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	iBgContext = CAknsBasicBackgroundControlContext::NewL( 
	   			KAknsIIDQsnBgAreaMain, aRect, ETrue);
	ActivateL();
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CWeatherForecastContainer::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CWeatherForecastContainer::ComponentControl( TInt aIndex ) const
	{
	switch ( aIndex )
		{
		case ELabel1:
			return iLabel;
		case ELabel2:
			return iLabelWeather;
		case EButton1:
			return iButtonBeijing;
		case EButton2:
			return iButtonShenyang;
		}
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CWeatherForecastContainer::SizeChanged()
	{
    //重置皮肤
    if(iBgContext)
		{
		//重置背景区域
		iBgContext->SetRect(Rect());
        
		//把Container在屏幕上的位置传递给iBgContext
		if ( &Window() )
			{
			iBgContext->SetParentPos( PositionRelativeToScreen() );
			}
		}
	CCoeControl::SizeChanged();
	LayoutControls();
	}
				
/**
 * Layout components as specified in the UI Designer
 */
void CWeatherForecastContainer::LayoutControls()
	{
	TRect rect = Rect();
	iLabel->SetExtent( TPoint( 0, rect.Height() * 2 / 10 ), 
			TSize( rect.Width() / 2, rect.Height() / 10 ) );
	iLabelWeather->SetExtent( TPoint( rect.Width() / 2, rect.Height() * 2 / 10 ), 
			TSize( rect.Width() / 2, rect.Height() / 10 ) );
	iButtonBeijing->SetExtent( TPoint( rect.Width() / 4, rect.Height() * 5 / 10 ),
			TSize( rect.Width() / 2, rect.Height() / 10 ) );
	iButtonShenyang->SetExtent( TPoint( rect.Width() / 4, rect.Height() * 7 / 10 ),
			TSize( rect.Width() / 2, rect.Height() / 10 ) );
	}

/**
 *	Handle key events.
 */				
TKeyResponse CWeatherForecastContainer::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}

/**
 *	Initialize each control upon creation.
 */				
void CWeatherForecastContainer::InitializeControlsL()
	{
	iLabel = new ( ELeave ) CEikLabel;
	iLabel->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_WEATHER_FORECAST_CONTAINER_LABEL1 );
		iLabel->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iLabel->SetAlignment( EHCenterVCenter );
		
	iLabelWeather = new ( ELeave ) CEikLabel;
	iLabelWeather->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_WEATHER_FORECAST_CONTAINER_LABEL2 );
		iLabelWeather->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iLabelWeather->SetAlignment( EHLeftVCenter );
	
	iButtonBeijing = static_cast< CAknButton* >
					( EikControlFactory::CreateByTypeL( EAknCtButton ).iControl );
	iButtonBeijing->ConstructFromResourceL ( R_WEATHER_FORECAST_CONTAINER_BUTTON1 );
	iButtonBeijing->SetContainerWindowL ( *this );
	iButtonBeijing->SetObserver( this ); 
	
	iButtonShenyang = static_cast< CAknButton* >
					( EikControlFactory::CreateByTypeL( EAknCtButton ).iControl );
	iButtonShenyang->ConstructFromResourceL ( R_WEATHER_FORECAST_CONTAINER_BUTTON2 );
	iButtonShenyang->SetContainerWindowL ( *this );
	iButtonShenyang->SetObserver( this ); 
	}

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CWeatherForecastContainer::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( EWeatherForecastContainerViewId ) )->ClientRect() );
	}
				
/**
 *	Draw container contents.
 */				
void CWeatherForecastContainer::Draw( const TRect& aRect ) const
	{
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
    MAknsSkinInstance* skin = AknsUtils::SkinInstance();
    MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
    AknsDrawUtils::Background( skin, cc, this, gc, Rect() );
	}

void CWeatherForecastContainer::HandleControlEventL( CCoeControl* aControl,
		TCoeEvent aEventType )

	{
	switch( aEventType )
		{
		case EEventStateChanged:
			RWeatherSession weather;
			User::LeaveIfError( weather.Connect() );
			TInt wea = -1;
			
			if( aControl == iButtonBeijing )
				wea = weather.GetWeather( EBeijing );
			else
				wea = weather.GetWeather( EShenyang );
			
			if( ESunny == wea )
				iLabelWeather->SetTextL( _L("Sunny") );
			else if( ECloudy == wea )
				iLabelWeather->SetTextL( _L("Cloudy") );
			iLabelWeather->DrawDeferred();
			
			weather.Close();
			break;
		default:
			break;
		}
	}


TTypeUid::Ptr CWeatherForecastContainer::MopSupplyObject( TTypeUid aId )
	{
	if (iBgContext )
		 {
		   return MAknsControlContext::SupplyMopObject( aId, iBgContext );
		 }
	return CCoeControl::MopSupplyObject(aId);
	}
